home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr20 / pcl570.zip / BATCH.TUT < prev    next >
Text File  |  1993-06-10  |  51KB  |  1,041 lines

  1.       
  2.        ----------------------------------------------------------------
  3.  
  4.               BATCH FILES FOR EFFICIENCY - GETTING REAL WORK DONE 
  5.  
  6.        ----------------------------------------------------------------
  7.  
  8.        Batch files are one of the hidden treasures within your 
  9.        computer. Let's face it, learning and using DOS commands is a 
  10.        test of patience, memory and stamina. Batch files are the secret 
  11.        weapon which can boost your computer into the fast lane of work 
  12.        productivity. 
  13.        
  14.        Batch files are small software programs which you can prepare in 
  15.        the space of only five or ten minutes which automate a variety 
  16.        of tasks and customize the computer to your work style. The 
  17.        bottom line is that batch files can preserve your sanity in the 
  18.        face of arcane, easily-forgotten DOS commands to provide you 
  19.        with a menu system for your hard drive, transfer and backup 
  20.        files, provide security, start other software programs, activate 
  21.        your printer and much more. 
  22.        
  23.        Amazingly, batch files require that you become familiar with 
  24.        only eight commands in addition to the normal DOS commands. 
  25.        Working with batch files means you are programming in the most 
  26.        literal sense - let's take a tour of the small miracles called 
  27.        batch files . . . 
  28.        
  29.        A batch file is little more than a list of DOS commands plus 
  30.        eight special batch commands. These commands are stored on your 
  31.        floppy or hard disk in an ordinary text file such as produced by 
  32.        your word processor or text editor. If you can type a letter to 
  33.        a friend, you can prepare a batch file! 
  34.               
  35.        Several time-saving batch file examples which provide real work 
  36.        power to your everyday computer problems are presented at the 
  37.        end of this tutorial. We will also examine the eight basic batch 
  38.        file commands as well as the AUTOEXEC.BAT file which starts your 
  39.        computer each morning. In addition we will present some simple 
  40.        tricks for managing your printer with batch files.
  41.           
  42.        One way to think about a batch file is that it takes the 
  43.        place of your keyboard and issues commands one after another 
  44.        until it reaches a conclusion. Batch files operate line by line 
  45.        and are read directly from the disk which makes them a little 
  46.        slow, but nevertheless useful and flexible. You can eliminate 
  47.        repetitious keyboard tasks by using batch files. Let's look at a 
  48.        short batch file . . .
  49.           
  50.        Each line of a batch file contains one instruction or operation 
  51.        per line which the computer is to perform. Below is the listing 
  52.        of a simple batch file example. Don't worry about understanding 
  53.        it yet, simply note that each instruction is a DOS command on a 
  54.        separate line. The list in the left column is the actual batch 
  55.        file, while the explanation in the right column is NOT part of 
  56.        the batch file, only a helpful column of comments. 
  57.  
  58.         EXAMPLE BATCH FILE                EXPLANATION
  59.                |                               |
  60.               date                         date displayed 
  61.               time                         time displayed
  62.               ver                          DOS version displayed
  63.               dir a:/p                     directory of a: floppy 
  64.                                            displayed with a pause
  65.  
  66.        The primary use of batch files is to automate sequences or 
  67.        instructions which you use frequently. A batch file always has 
  68.        the extension BAT. A batch file might for instance be named
  69.        MENU.BAT, CAR.BAT, INSTALL.BAT or MONEY.BAT.
  70.  
  71.        Each line in a batch file is a separate command and is performed 
  72.        in sequence as if you had typed in the command from your 
  73.        keyboard at the DOS prompt. In addition to the usual DOS 
  74.        commands, batch files can also contain additional special 
  75.        commands to provide truly sophisticated program structures which 
  76.        include decision branching and even repetitions of commands. 
  77.        In addition, batch files may have special parameters or inputs 
  78.        passed to them at the time you run the batch file from the DOS 
  79.        command line or prompt. 
  80.        
  81.        A batch file is run or started by typing the file name without 
  82.        the extension. This of course also applies to files ending with 
  83.        file extensions EXE or COM as well as BAT.
  84.  
  85.        Example: A>hello     (Then pressing enter or return key)
  86.        This starts the file   hello.bat hello.com or hello.exe
  87.  
  88.        Example: C>whoops    (Then pressing enter or return key)
  89.        This starts the file  whoops.exe whoops.bat or whoops.com
  90.        
  91.        There are several ways to abort or terminate any batch file in 
  92.        progress. 1) Issue the break command which uses the two key 
  93.        combination CONTROL-BREAK (hold down the control or CTRL key 
  94.        then press the break key) or you can 2) Tap CTRL-SCROLL LOCK 
  95.        keys or 3) Tap CTRL-C keys. 
  96.  
  97.        There are many ways to prepare a batch file, all of which use 
  98.        simple methods of text editing or word processing:  1) Use the 
  99.        DOS COPY CON (copy console) command. 2) Use the older EDLIN line 
  100.        editor available within DOS. 3) Use the newer DOS EDIT text 
  101.        editor available in DOS version 5.0 4) Use any word processor 
  102.        (e.g., Microsoft Word, Wordperfect, PC-Write) whose output has 
  103.        been set to ASCII or pure text output - many word processors use 
  104.        a "save as" file option to select pure ASCII output. See your 
  105.        word processor reference book index under ASCII file saving. 
  106.        
  107.        Let's prepare a batch file: 
  108.  
  109.        First make sure you have a formatted disk in your disk drive and 
  110.        DOS is displaying a DOS prompt such as A> or C>. We need a disk 
  111.        in order to save our batch file.
  112.        
  113.        We will be using the command COPY CON (copy data from the 
  114.        CONsole) command. We could also use any ASCII (plain english) 
  115.        text word processor (e.g., Wordperfect) or even EDLIN on your 
  116.        DOS disk. Note that you can use either upper or lower case to 
  117.        prepare batch files (capitals or small letters.) Using COPY CON 
  118.        is like using a small typewriter to prepare your batch file. 
  119.  
  120.        Type the following list carefully at the DOS prompt:
  121.  
  122.        copy con blink.bat     (press enter - cursor skips to new line)
  123.        echo Hello there       (press enter)
  124.        ver                    (press enter)
  125.        date                   (press enter)
  126.        dir/p                  (press enter)
  127.        ^Z                     (press F6 OR your can press control key 
  128.                               AND Z key, then press enter)
  129.  
  130.        When done, you'll have prepared a batch file of DOS commands 
  131.        named blink.bat. Run the batch file by typing this at the DOS 
  132.        prompt: 
  133.                           blink    (then press enter key)
  134.        
  135.        WARNING! Be careful when preparing batch files since you will 
  136.        automatically overwrite and destroy any PREXISTING batch files 
  137.        of the same name! Better to make a backup copy of the existing 
  138.        batch file (or rename it temporarily with the REN command) and 
  139.        then proceed. A classic beginner mistake is to tinker with the 
  140.        crucial AUTOEXEC.BAT file without saving a backup copy first!
  141.        More about AUTOEXEC.BAT later in this tutorial. 
  142.  
  143.        We could also have named the batch file above hello.bat or 
  144.        info.bat rather than blink.bat by changing the first line we 
  145.        typed, but for simplicity we'll stick with blink.bat which does 
  146.        the following chores: Print "hello there" on the screen, then 
  147.        type the DOS version in use then display date and finally 
  148.        produce a directory listing with pause after each screenful. At 
  149.        this point the batch file ends and returns you to DOS. In the 
  150.        first line we use COPY CON as our small word processor to begin 
  151.        construction of the batch file named blink.bat. In the last line 
  152.        the ^Z means end of batch file preparation - exit back to DOS 
  153.        and save the file on disk. 
  154.  
  155.        Another example batch file for you to try, let's call it F.BAT
  156.        This is a reminder that F.BAT refers to formatting a disk:
  157.  
  158.        echo off                                        
  159.        copy con f.bat                                                 
  160.        cls                                                            
  161.        pause                                                          
  162.        format b:                                                      
  163.        echo all done                                                  
  164.  
  165.        Notice that here I have omitted the COPY CON command to start 
  166.        file preparation and the F6 to end file preparation. Use the 
  167.        COPY CON method described above if you wish or whatever word 
  168.        processing software is available to construct the file. 
  169.        
  170.        This batch file (activated by typing f then enter) will clear 
  171.        the screen then prepare to format a blank disk in b: drive. 
  172.        NOTE: you MUST have FORMAT.COM, the DOS formatting utility, on 
  173.        the same disk as the batch file, f.bat - remember that format is 
  174.        an EXTERNAL command and f.bat will try to find FORMAT.COM. 
  175.        
  176.        After the batch file has formatted the disk it prints "all done" 
  177.        on the screen. So instead of LOTS of keystrokes to format a 
  178.        disk, you just tap "F" then hit enter and the batch file runs. 
  179.        See how we are saving keystrokes - that's one of the purposes of 
  180.        a batch file! We will discuss the new ECHO command a little 
  181.        later in this tutorial. 
  182.        
  183.        A reminder: Ctrl-Break or Ctrl-C key combination will halt 
  184.        any batch file operation if you wish.
  185.  
  186.        The next batch file might be used to backup word processing data 
  187.        files from your hard drive onto a floppy disk. Let's make an 
  188.        initial assumption that your word processing documents are 
  189.        stored on your hard drive in the subdirectory C:\DOC. You could 
  190.        name this backup batch file B.BAT and when you need to backup 
  191.        simply type B (then press enter) at the DOS prompt. We've 
  192.        omitted the copy con command at the top of the file and the ^Z 
  193.        at the end of the file since you already know how to start and 
  194.        end a batch file from previous examples. Note the new commands 
  195.        we are using: REM, ECHO and PAUSE which we will discuss shortly. 
  196.  
  197.        ECHO OFF
  198.        ECHO This batch file backs up DOCUMENT files to disk B:
  199.        ECHO READY TO BACKUP. 
  200.        PAUSE                                                                  
  201.        COPY C:\DOC\*.* B:
  202.        ECHO All done!                                                          
  203.  
  204.        The line which does most of the work is COPY C:\DOC\*.* B: 
  205.        which translates as "copy all files from C:\DOC subdirectory 
  206.        and transfer them to B: drive."
  207.  
  208.        One batch file can start or call another, but the original batch 
  209.        file cannot usually be returned to - you must continue on within 
  210.        the second batch file. For example, you could have one batch 
  211.        file start another batch file.
  212.  
  213.        If a batch file contains a typing or syntax error in any of its 
  214.        commands, the computer will stop execution at that point and 
  215.        return you to DOS which remembers which disk contains the batch 
  216.        file and the drive it was in. If you remove the original disk, 
  217.        DOS will ask you to replace it so it can finish executing the 
  218.        batch file. Batch files execute one step at a time from the disk 
  219.        and NOT from RAM memory. This disk-based nature of batch files 
  220.        make them a little slow, but they get the work done in 
  221.        reasonably short order for most people.
  222.  
  223.        Several books and power user tricks should also be mentioned 
  224.        regarding batch files before we move on . . .
  225.  
  226.        A superlative book on batch files you might wish to investigate 
  227.        is MOS-DOS Batch File programming by Ronny Richardson, 1988,
  228.        Wincrest Books.
  229.       
  230.        You should also investigate the SEBFU (Scanlon Enterprises Batch 
  231.        File Utilities) software package which is a series of small 
  232.        batch file utilities which offer an improvement over the 
  233.        standalone DOS batch file programming language. SEBFU allows the 
  234.        user to produce subtle, powerful batch files and includes an 
  235.        excellent tutorial about using batch files for productivity. If 
  236.        you wish to try SEBFU, the shareware version, contact Scanlon 
  237.        Enterprises, 38354 17th ST E #C, Palmdale, CA 93550 Telephone 
  238.        (805) 272-4827. Include five dollars for shipping and handling. 
  239.        
  240.        Special batch file COMPILER utilities exist which speed 
  241.        execution of batch files and make them run from RAM memory 
  242.        rather than disk. Most computer clubs and BBS system carry these 
  243.        batch file compilers. One popular batch compiler is named 
  244.        BAT2EXEC and was produced by PC Magazine several years ago. 
  245.        Batch files will FLY once they have been compiled and run 
  246.        from memory rather than disk. Another batch file speedup trick 
  247.        uses a "ramdisk" as follows.
  248.  
  249.        Remember the DOS VDISK command in our second DOS tutorial? Many 
  250.        DOS experts put commonly used batch files in a virtual or RAM 
  251.        disk in memory where a batch file runs quickly. This is one 
  252.        trick which can turbocharge batch file operations. 
  253.  
  254.        ----------------------------------------------------------------
  255.  
  256.           AUTOEXEC.BAT FILE BASICS - THE WAKEUP CALL TO YOUR COMPUTER 
  257.        
  258.        ----------------------------------------------------------------
  259.       
  260.        The AUTOEXEC.BAT file starts your computer exactly the way you 
  261.        want. It allows you to customize the machine to your liking as 
  262.        the computer comes to life. You can cause the AUTOEXEC.BAT file 
  263.        to print a startup menu of choices, load one particular program, 
  264.        execute another batch file or other useful tasks. The 
  265.        AUTOEXEC.BAT file is the first file DOS runs after loading 
  266.        itself and configuring the computer. The AUTOEXEC.BAT file must 
  267.        be on the same disk as DOS when the computer starts. 
  268.  
  269.        The AUTOEXEC.BAT file is a special batch file which MUST be 
  270.        placed in the main or root directory of a disk to function 
  271.        properly. 
  272.  
  273.        An AUTOEXEC.BAT file can always be modified, enlarged, edited, 
  274.        or deleted later as you wish. Sometimes it is useful to have 
  275.        several AUTOEXEC.BAT files. Each on a different startup disk to 
  276.        operate different programs! An AUTOEXEC.BAT file, like all batch 
  277.        files, can be modified with any word processor, DOS EDIT or 
  278.        EDLIN text editor. 
  279.  
  280.        Before tinkering with your AUTOEXEC.BAT file, make sure you do 
  281.        not accidentally over-write or destroy your current AUTOEXEC.BAT 
  282.        file. If necessary, rename your current AUTOEXEC.BAT file (using 
  283.        the rename or REN command) and make a new file while saving the 
  284.        old one "just in case." Never edit files on your original DOS disk, 
  285.        work on a copy! This wise advice applies to ANY computer file.
  286.        Save a backup copy; never work on the original.
  287.  
  288.        Examine the next batch file:
  289.  
  290.        copy con AUTOEXEC.BAT              (press enter)
  291.        123                                (press enter)
  292.        ^Z                                 (press enter)
  293.  
  294.        This means (first line) create a file named AUTOEXEC.BAT as 
  295.        typed from the keyboard or con (console). Then (second line) 
  296.        start program named 123. The (final line) end of batch file 
  297.        preparation - stash it on the disk. Since the first and last 
  298.        lines prepare the batch file, this program really has only one 
  299.        line whose purpose is to start a specific program (123.EXE) each 
  300.        time the computer is turned on.
  301.  
  302.        When finished you'll see a file named AUTOEXEC.BAT on your 
  303.        directory listing screen which contains automatic startup 
  304.        instructions. If this file were placed on your main DOS disk it 
  305.        would try to start a program such as 123.EXE if such a program 
  306.        existed there. And since it is AUTOEXEC.BAT this would be the 
  307.        first file run each morning when you turn on your computer.
  308.  
  309.        You can also start the AUTOEXEC.BAT by typing autoexec and then 
  310.        pressing enter. To take a "peek" at the contents of an 
  311.        AUTOEXEC.BAT file (or any bat file) simply use the type command. 
  312.        Remember to use Ctrl-S key combination to pause the screen if 
  313.        the display flashes by too quickly. 
  314.  
  315.        Example: C>type AUTOEXEC.BAT       (display file contents)
  316.        Example: A>type b:AUTOEXEC.BAT     (display file on the B: drive)
  317.        Example: C>type AUTOEXEC.BAT>PRN   (display file contents on 
  318.                                            printer)
  319.        
  320.        Here is another AUTOEXEC.BAT file, this time from a hard drive 
  321.        computer. It provides a higher degree of control and direction 
  322.        that a computer user might need for hard drive customization. 
  323.  
  324.        path \dos;\reflex;\wp;\util;\doc;\nor;\bat                     
  325.        prompt $P$G                                                    
  326.        cpu n                                                          
  327.        verify on                                                      
  328.        blank                                                          
  329.        mode bw80,r                                                    
  330.        dispclk                                                        
  331.        type menu.txt                                                  
  332.  
  333.        Let's examine this more complicated AUTOEXEC.BAT file in greater 
  334.        detail: 
  335.  
  336.        The first line after establishes a path command to help DOS 
  337.        search every subdirectory on the hard disk -you don't have to 
  338.        switch around to different areas of the disk, DOS will search 
  339.        for you since it knows the various subdirectory "paths" to take. 
  340.  
  341.        The second line alters the cursor prompt to always display your 
  342.        current location and subdirectory. Instead of seeing C> you view
  343.        a more informative C:\DOCS> for example.
  344.  
  345.        The third line is a reference to the speed the computer will 
  346.        operate at and is a unique command to a particular brand of 
  347.        machine (cpu n means start the central processing unit chip at 
  348.        normal speed.) Cpu is really CPU.COM, an external file which 
  349.        sets the computer's processing speed. Your DOS disk may or may 
  350.        not contain the file CPU.COM. This highlights the ability of the 
  351.        AUTOEXEC.BAT file to start or load other programs and is 
  352.        very useful!
  353.  
  354.        The fourth line turns on the verify function for file copying. 
  355.  
  356.        The next line instructs the DOS mode function to switch to black 
  357.        and white display, 80 columns wide and shift one column to the 
  358.        right for alignment. We are setting the hardware the way we 
  359.        wish. We could also configure the modem or printer with the mode 
  360.        command.
  361.  
  362.        Next we ask DOS to tell us the time and date. Run the program 
  363.        DISPCLK.COM, an external program stored on disk.
  364.   
  365.        The final line instructs DOS to type to the screen a text file 
  366.        containing a simple menu for the monitor to display. Menu.txt
  367.        probably gives us choices of programs and thus calls other batch 
  368.        files. 
  369.  
  370.        ---------------------------------------------------------------- 
  371.  
  372.                           BATCH FILE COMMANDS AND USE 
  373.  
  374.        ---------------------------------------------------------------- 
  375.  
  376.        In addition to the normal DOS commands, batch files have eight 
  377.        special subcommands. At the end of this section we will provide 
  378.        some interesting batch files which you can use or edit on your 
  379.        computer. 
  380.        
  381.        The special batch commands are: 
  382.  
  383.        --- REM --- The rem command sends a message to the screen or 
  384.        simply documents or notes a part of a batch file's operation. 
  385.        You should use REM extensively to document long detailed batch 
  386.        files so you can revise things and locate portions of the 
  387.        program if you decide to change the batch file later. Remarks 
  388.        can be up to 123 characters long. REM does not cause any 
  389.        operation, it merely documents what you want to say or do.          
  390.  
  391.        Example: C>REM this is the location of menu operations 
  392.  
  393.        In DOS 2.0 the REM command could be replaced with a period or 
  394.        dot, but this is not true in DOS 3.0 and above. 
  395.  
  396.        Example:  C>. this is the location of menu operations 
  397.  
  398.        --- PAUSE --- Stops batch file execution on a temporary basis 
  399.        until you press a key. Thus you can pause a batch file and do 
  400.        some operation (perhaps changing a floppy disk) and then 
  401.        continue when you strike a key. Very useful.
  402.  
  403.        Example: B>PAUSE   
  404.        Example: B>PAUSE This is an optional message, pardner! 
  405.  
  406.        In the first example, no message is displayed.
  407.  
  408.        --- ECHO --- Turns display listing of commands on/off. It can 
  409.        also send a message to the screen. It is frequently turned off 
  410.        to remove excessive screen messages. Normally, with ECHO on, 
  411.        screen messages are sent to the screen which can be distracting. 
  412.        To suppress them use the first example. To restart the messages 
  413.        use the second example. To add a message with the ECHO command 
  414.        see example three. REM or remark command can also send a message 
  415.        to the screen but NOT with ECHO turned off! 
  416.  
  417.        Example:   A>ECHO OFF 
  418.        Example:   A>ECHO ON 
  419.        Example:   A>ECHO It's raining cats, dogs and computers
  420.        Example:   A>@ECHO (don't display this particular line)
  421.        
  422.        --- PARAMETERS AND MARKERS ----  This is NOT a batch file 
  423.        command like ECHO or PAUSE. 
  424.  
  425.        Instead parameters are additional pieces of information or 
  426.        "modifiers" which follow DOS commands. 
  427.  
  428.        Example:   C>format b:/s     
  429.  
  430.        In the above, format is the command while b: and /s are the 
  431.        parameters. Parameters modify the basic operation of a DOS 
  432.        command but are not required by the command to operate. A batch 
  433.        file can also accept parameters such as a word, filename, 
  434.        symbol, drive letter or any useful character or group of 
  435.        characters! 
  436.  
  437.        Markers placed inside the batch file listing signify which 
  438.        parameter goes where. Markers are made from a percent sign (%) 
  439.        and a single digit between 0 and 9 for a total of ten markers 
  440.        available (remember, zero is a number too.) Here are the ten 
  441.        markers: 
  442.  
  443.        %0    %1    %2    %3    %4    %5    %6    %7    %8    %9 
  444.  
  445.        Let's use an example. Pretend that DOLITTLE.BAT is on your 
  446.        floppy. Within its listing of commands there might be this 
  447.        single line: 
  448.  
  449.        ECHO %0 %1 %2      (ECHO shows messages on the monitor) 
  450.  
  451.        If at the DOS prompt you typed:  
  452.        
  453.        B>DOLITTLE fancy pants  (then press enter) 
  454.  
  455.        Your screen would show the following:   
  456.                   
  457.                           ECHO DOLITTLE fancy pants. 
  458.  
  459.        In this case, %0 has taken on the value at the start of the DOS 
  460.        command which is the first word "DOLITTLE". Meanwhile %1 has 
  461.        become "fancy" and %2 is now pants. 
  462.  
  463.        Looking at this another way: 
  464.  
  465.                         DOLITTLE   fancy  pants 
  466.                          |          |       | 
  467.                  ECHO    %0         %1      %2    
  468.  
  469.        Let's try a more useful example. Pretend you had a large file of 
  470.        word processing files containing bills you have to pay from time to 
  471.        time. 
  472.  
  473.        You need to look up bills or amounts in the file accounts.txt 
  474.        which is in plain ASCII (english) text from your word processor. 
  475.        
  476.        The DOS FIND utility can search large files for specific words, 
  477.        strings or characters. The general format for the FIND command 
  478.        is: FIND "text" filename. FIND is located in the file FIND.COM
  479.        on your DOS disk and must be present with the batch file to be
  480.        used.
  481.  
  482.        A simple batch file possibly named GET.BAT could do this: 
  483.        
  484.        ECHO OFF
  485.        ECHO searching for data . . . . 
  486.        FIND "%1" %2 
  487.        ECHO Finished, boss 
  488.  
  489.        Start the batch file get.bat with search data like this: 
  490.  
  491.        C>get grocery accounts.txt    (first word starts get.bat, second 
  492.        word is the item to search for, third item is the file to 
  493.        search.) 
  494.        
  495.        As a result, you will get a report of the line where the word 
  496.        "grocery" is found within the file accounts.txt. This could also 
  497.        be used to search a telephone list or list of employee names and 
  498.        addresses. A powerful idea for a short batch file! 
  499.  
  500.        --- GOTO ---  Jumps to a labeled set of commands within the 
  501.        batch file. The general format for the command is    GOTO LABEL 
  502.        where LABEL is a line in the batch file which must start with a 
  503.        colon (:) followed by a name up to eight characters long. 
  504.  
  505.        A simple, but useless batch file illustrates the GOTO command by 
  506.        looping around in circles doing the same task endlessly.            
  507.                                                             
  508.        Example listing for batch file: 
  509.  
  510.        ECHO OFF
  511.        :kitty 
  512.        ECHO watch this fill your screen over and over, folks 
  513.        GOTO kitty 
  514.  
  515.        Note! On some versions of DOS it is necessary to include one blank 
  516.        line at the end of this file. In the above example, just press 
  517.        Enter/Return key one extra time after the line "GOTO kitty"
  518.        and then save the batch file.
  519.  
  520.        The above batch file will continue to print the remark line over 
  521.        and over since it always returns to the start. Tap Ctrl-Break to 
  522.        stop this silliness. The true usefulness of the GOTO command is 
  523.        best understood by allowing the GOTO within a batch file to 
  524.        transfer control elsewhere within its listing rather than to the 
  525.        line immediately next in sequence. You can thus cause varying 
  526.        useful results depending on a conditions present. Choices and 
  527.        different outcomes are a trademark of savvy batch file use.
  528.  
  529.        --- IF ---  Allows conditional operation of a command. This is a 
  530.        fancy way of saying you can cause a batch file to make decisions 
  531.        based on a logical condition or input then do something. The 
  532.        usual syntax of the IF command is IF CONDITION COMMAND. Let's 
  533.        take this apart and examine the concept. 
  534.  
  535.        In the situation IF CONDITION COMMAND: 
  536.  
  537.        COMMAND is any normal DOS or batch file command and CONDITION is 
  538.        one of three possible tests that yield true or false.         
  539.  
  540.        Example:  IF %1==w GOTO dog        (we'll explain this in a bit) 
  541.        Example:  IF %3 == 80 MODE BW80    (we'll explain this in a bit) 
  542.  
  543.        The three possible tests are: 
  544.  
  545.        1. The ERRORLEVEL condition (i.e., a specific number is found). 
  546.        2. The STRING COMPARISON. (i.e., two strings are equivalent or 
  547.        not.) 
  548.        3. The FILE EXISTENCE condition. (i.e., if a file exists or not.) 
  549.  
  550.        In true full-featured programming languages many other logical 
  551.        tests might be allowed, but for batch files these are the 
  552.        only three tests. Let's examine the three more closely. Then 
  553.        illustrate with an example.
  554.  
  555.        1. ERRORLEVEL is a number which tells DOS whether the last 
  556.        program run was successful. If so the errorlevel is zero (0) 
  557.        anything else above zero means unsuccessful. 
  558.  
  559.        2. STRING COMPARISON, the second conditional test, is always 
  560.        indicated in a batch file by double equals signs (==). A test is 
  561.        designated by the condition  IF string1 == string2. This is 
  562.        frequently used with parameters or markers such as:  IF %3 == 80 
  563.        MODE BW80. 
  564.  
  565.        3. In the final and third conditional test, FILE EXISTENCE, the 
  566.        usual format is IF EXIST d:filename.ext. which checks for a 
  567.        certain file on a certain drive. You can thus check for a 
  568.        certain disk or file before continuing the batch file process. 
  569.        Pathnames are not allowed (d:\slip\and\slide). 
  570.  
  571.        Let's try a batch file example to illustrate the use of STRING 
  572.        COMPARISONS to make a choice in how the batch file does its 
  573.        work. In a way, this is a menu program. Pretend you have two 
  574.        software applications. One is a word processor named WORD.EXE 
  575.        whose command to start is WORD and the other is a spreadsheet 
  576.        named LOTUS.EXE whose command is LOTUS to start. 
  577.  
  578.        If we prepared a simple batch file called go.bat whose listing is 
  579.        below, we could start one or the other program by using either 
  580.        the command:  
  581.  
  582.                      A>go w  (to start the word processor)  
  583.  
  584.                                    OR THIS:
  585.  
  586.                       A>go s  (to start the spreadsheet). 
  587.  
  588.        Notice how the "w" or "s" is picked up by the batch file and 
  589.        sends the program either one direction or the other in the 
  590.        example below. The remarks lines which begin with REM in the 
  591.        batch file give you a clue about the operation of the program 
  592.        but are not themselves commands. The end result of this batch 
  593.        file is a saving of keystrokes for frequently used software (the 
  594.        word processor and spreadsheet) and could be expanded to start 
  595.        many other software packages. 
  596.       
  597.        REM This batch file selects one of two choices based on input
  598.        REM The next line turns off screen echo to avoid screen clutter
  599.        ECHO OFF
  600.        REM Begin test for one of two choices
  601.        REM Next two lines use percent signs as markers for "w" or "s" keys 
  602.        IF %1==w GOTO dog
  603.        IF %1==s GOTO cat
  604.        REM Next line forces goto end if no match is made for w or s 
  605.        GOTO end
  606.        :dog
  607.        REM Next command starts word processor, WORD.EXE
  608.        WORD
  609.        GOTO end
  610.        :cat
  611.        REM Next command starts spreadsheet, LOTUS.EXE
  612.        LOTUS
  613.        GOTO end
  614.        :end
  615.        REM Next line switches to root directory and ends the batch file
  616.        CD\   
  617.        ECHO Batch file done, bye bye!
  618.        
  619.        --- SHIFT ---  Re-assigns the relationship of parameters to 
  620.        markers. It changes their values. And it does it in a very odd 
  621.        way . . . 
  622.  
  623.        Remember that there are only ten markers available to a batch 
  624.        file to hold the parameter values as we mentioned above. Here 
  625.        they are: 
  626.  
  627.        %0    %1    %2    %3    %4    %5    %6    %7    %8    %9 
  628.  
  629.        However you can raise the limit of 10 parameters in a batch file 
  630.        using the single word SHIFT. When this command is encountered in 
  631.        a batch file, all the parameter and marker pairings are shifted 
  632.        one unit to the left. Whatever was assigned to %0 is lost. 
  633.  
  634.        A diagram to visualize. Before a SHIFT command is issued the 
  635.        parameters and markers might be: 
  636.  
  637.                 %0    %1    %2    
  638.                  |     |     |
  639.                 dog   cat    computer
  640.  
  641.        After the SHIFT command we would see:
  642.  
  643.                 %0    %1          %2    
  644.                  |     |          |
  645.                 cat   computer
  646.  
  647.        Notice that dog is lost, %1 becomes computer and %2 is left 
  648.        vacant unless it takes a new parameter from %3 (if %3 had a 
  649.        parameter). The effects of the SHIFT command are wide ranging 
  650.        throughout the batch file and provide great flexibility and a 
  651.        range of parameters greater than ten values. 
  652.  
  653.        --- FOR..IN..DO ---    Allows iteration (repetition) of actions 
  654.        or commands. The command is similar to a FOR...NEXT...STEP loop 
  655.        programmers use. This command lets you repeat an action several 
  656.        times.
  657.  
  658.        The command is rather subtle and could be thought of as a three 
  659.        part command. The syntax is: 
  660.  
  661.        FOR %%Variable IN (Set) DO Command 
  662.  
  663.        Let's look more closely at the three parts: 
  664.  
  665.           FOR %%Variable       IN (Set)    DO Command
  666.           ==============       =======     ==========
  667.               |                    |           |         
  668.             part 1              part 2     part 3    
  669.  
  670.        Translating into English this means: FOR a certain batch file 
  671.        variable withIN a SET of filenames or commands DO a certain 
  672.        action. 
  673.  
  674.        The %%VARIABLE is a one-letter variable which must have a double 
  675.        %% prior to the letter to distinguish it from single % markers 
  676.        we have seen earlier. 
  677.  
  678.        The SET portion of the command is always in parenthesis as 
  679.        (SET). The SET represents filenames or DOS commands you want the 
  680.        %% variable to assume while the command is executing. A space is 
  681.        used between entries. Pathnames are never allowed but wildcards 
  682.        such as *.* are acceptable. If the SET contains DOS command then 
  683.        only the %%VARIABLE is used. 
  684.               
  685.        The COMMAND is a DOS command or batch subcommand. One or several 
  686.        of these commands will contain the %%Variable in it. 
  687.  
  688.        Let's try an example. Pretend by you want a batch file to 
  689.        present the DOS version then clear the screen and finally issue 
  690.        the directory. We could do this in three lines by: 
  691.        
  692.        VER
  693.        CLS
  694.        DIR/P
  695.        
  696.        However, with the command FOR..IN..DO we can do this in one 
  697.        line:
  698.  
  699.        FOR %%T IN (Ver cls Dir/P) DO %%T 
  700.  
  701.        Notice how each DOS command is separated by a space. ? and * are 
  702.        NOT allowed within any command within the SET. Use a colon : 
  703.        instead of a space within the set when passing parameters to 
  704.        programs. You can issue the FOR..IN..DO batch file subcommand at 
  705.        the DOS prompt by dropping one of the percentage signs  %  on 
  706.        the variable. Let's move on to some practical and fairly 
  707.        interesting examples . . . 
  708.  
  709.        ---------------------------------------------------------------- 
  710.  
  711.                       BATCH FILE PROJECTS FOR YOU TO TRY!
  712.  
  713.        ---------------------------------------------------------------- 
  714.  
  715.        The following batch files do real work and can teach you some 
  716.        fascinating principles. 
  717.        
  718.        To save typing each example, here's a shortcut: simply load this 
  719.        file, BATCH.TUT, from the PC-Learn disk or hard drive into your 
  720.        word processor. Next, delete the tutorial and batch files you 
  721.        don't need, keeping only the batch file lines you require. Move 
  722.        all lines of the batch file to the far left margin then save the 
  723.        batch file back to disk in plain ASCII text with a permanent 
  724.        batch file name such as CANDY.BAT, GO.BAT or PRINTER.BAT which 
  725.        you can easily remember. Feel free to change or add things to 
  726.        these batch files with your word processor since that's the 
  727.        point - batch files are flexible! 
  728.        
  729.        Note the liberal use of the remarks or REM lines in some batch 
  730.        files to explain how things work. If you like, delete all REM 
  731.        lines to save typing time, space and speed program execution! 
  732.  
  733.                       ---- Fast freespace batch file ----
  734.        
  735.        This is a short but useful batch file. It reports the amount of 
  736.        freespace on a disk by using the FIND command in DOS to search 
  737.        out the line containing the word "free" in the DOS DIR command.
  738.  
  739.                             Program name: TELLFREE.BAT
  740.        
  741.        ECHO OFF
  742.        CLS
  743.        ECHO CHECKING FOR FREE SPACE ON DISK
  744.        DIR | FIND "free"
  745.  
  746.        You could modify the last line to DIR A: | FIND "free" to locate 
  747.        the free space on the A: drive. The piping symbol | , discussed 
  748.        in a previous DOS tutorial illustrates how one command (DIR) can 
  749.        "pipe" its output into the FIND command. You MUST have the DOS 
  750.        file FIND.EXE on the same disk so that the DIR command can use 
  751.        it. The word "free" is case sensitive and must MOT be typed in 
  752.        capital letters since the FIND command is case sensitive. 
  753.        
  754.        Obviously, to use this small program, at the DOS prompt just 
  755.        type TELLFREE then press the return or enter key. You could also 
  756.        name this batch file T.BAT if you wanted to only type a 
  757.        single letter and save additional keystrokes. 
  758.  
  759.                       ---- Fast deletion batch file ----
  760.        
  761.        This is a short batch file with some powerful wrinkles for 
  762.        speedy file deletions. It could be modified in many ways.
  763.  
  764.                               Program name: DB.BAT
  765.        
  766.        ECHO OFF
  767.        CLS
  768.        ECHO Ready to delete ALL files on B: drive
  769.        ECHO Press control-break keys to abort or
  770.        ECHO any other key to continue
  771.        PAUSE
  772.        REM Following line does the deleting
  773.        ECHO Y | DEL B:*.*
  774.        ECHO DONE!
  775.  
  776.        This is an odd batch file using some powerful DOS secrets. Line 
  777.        four reminds us we are about to delete all files on the B: drive 
  778.        and gives us the chance to abort using the control-break key 
  779.        combination which works to abort all batch files. The pause 
  780.        command on line six halts operations and waits for our keypress. 
  781.        
  782.        Line eight shows the real power of DOS in a one line command 
  783.        which uses ECHO to pass the keystroke Y (meaning yes) via the 
  784.        pipe operation of DOS represented by the vertical bar |. This 
  785.        effectively means that the deletion of all files on B: drive 
  786.        represented by *.* will take place WITHOUT pausing for the 
  787.        traditional Yes/No request. The name of the batch file, DB.BAT 
  788.        reminds us that its use is to delete all files on B: drive or 
  789.        DB, for short! 
  790.               
  791.                       ---- Fast formatting batch file ----
  792.        
  793.        This batch file will speed your disk formatting. Its name 
  794.        F.BAT means format disks, and it gives you some choices and 
  795.        illustrates other batch file tricks.
  796.        
  797.        There are three ways to use it: for formatting A: drive, B: 
  798.        drive, or both A: and B: drives. Note the minimum number of 
  799.        keystrokes required and how the batch file determines your 
  800.        choice by using parameters. This batch file also requires a 
  801.        small text file called yes.txt which shows how a file can be 
  802.        used to redirect input to a DOS command in place of the 
  803.        keyboard. It is explained below. Be sure to prepare YES.TXT or 
  804.        F.BAT will not work!
  805.  
  806.        To start this batch file you MUST chose one of the following. 
  807.        The space between letter characters is important.
  808.  
  809.        To format only A: drive, at DOS prompt enter   F A
  810.        To format only B: drive, at DOS prompt enter   F B
  811.        To format both A and B drives, at DOS prompt enter   F AB
  812.        
  813.        In the above commands, the A, B or AB will be inserted into the 
  814.        batch file in the location of the symbol %1 as noted earlier in 
  815.        this tutorial.
  816.        
  817.                              Program name: F.BAT
  818.        
  819.        ECHO OFF
  820.        CLS
  821.        ECHO FORMATTING DISKS NOW! 
  822.        REM This batch file selects one of three choices based on input
  823.        REM Begin test for one of three choices
  824.        IF %1==A GOTO DOG
  825.        IF %1==B GOTO CAT
  826.        IF %1==AB GOTO MOUSE
  827.        REM Next line forces goto end if no match is made 
  828.        GOTO end
  829.        :DOG
  830.        FORMAT A:<yes.txt
  831.        GOTO end
  832.        :CAT
  833.        FORMAT B:<yes.txt
  834.        GOTO end
  835.        :MOUSE
  836.        FORMAT A:<yes.txt
  837.        FORMAT B:<yes.txt
  838.        GOTO end
  839.        :end
  840.        
  841.        Lines 18 and 19 provide formatting of B: immediately after 
  842.        formatting of A: is finished. A short but very powerful batch 
  843.        file. 
  844.        
  845.        Note, how the batch file cleverly uses redirection with the 
  846.        < symbol discussed in our earlier DOS tutorial to send the 
  847.        "stored keystrokes" in the file yes.txt to the format command so 
  848.        you do NOT need to type Yes/No each time the computer formats a 
  849.        disk which is usual when using the format command. Redirection 
  850.        is a very powerful DOS operation. You will also need the file 
  851.        YES.TXT as discussed below. 
  852.  
  853.                              Program name: YES.TXT
  854.  
  855.        This is a very simple file. Start your word processor, DOS Edlin 
  856.        or even use the COPY CONsole command earlier. This tiny file 
  857.        contain a "y" character and two carriage returns and serves as 
  858.        an input file to take the place of the keyboard as explained 
  859.        above. It provides the format command with "simulated" keyboard 
  860.        response of Y (Yes) so the batch file above can continue.
  861.  
  862.        Using Copy Console command (which is one method to prepare
  863.        YES.TXT):
  864.        
  865.        COPY CON YES.TXT     (press enter)
  866.        y                    (press enter)
  867.                             (press enter, skip to new line)
  868.                             (press enter, skip to new line)
  869.        ^Z                   (press F6 to end and write file to disk)
  870.        
  871.                    ---- Make a menu batch file project ----
  872.        
  873.        The next project is actually four simple batch files which work 
  874.        together to provide a "poor man's" menu program for a hard 
  875.        drive. Note the automatic switching between subdirectories 
  876.        provided by the files 1.bat, 2.bat and 3.bat. Obviously you 
  877.        could expand this to include menu choices for more options. Type 
  878.        in each batch file program and save on your hard drive. Place 
  879.        all files in the root directory of your hard drive, usually C:\ 
  880.        When ready to start, simply type M, then press enter key. Notice 
  881.        how M.BAT runs one of the other three files, 1.BAT, 2.BAT or 
  882.        3.BAT. Notice also how when those batch files are done, they 
  883.        automatically run M.BAT to return from where they began and 
  884.        again display the menu screen.
  885.  
  886.                               Program name: M.BAT
  887.        
  888.        REM Third line turns off echo so commands are not repeated to
  889.        REM the screen twice, avoids screen clutter
  890.        ECHO OFF
  891.        REM Next line clears the screen
  892.        CLS
  893.        REM Next line suggests option 1 which will run 1.bat 
  894.        ECHO PRESS 1 FOR WORD PROCESSOR
  895.        REM Next line suggests option 2 which will run 2.bat 
  896.        ECHO PRESS 2 FOR SPREADSHEET
  897.        REM Next line suggests option 3 which will run 3.bat 
  898.        ECHO PRESS 3 FOR DATABASE
  899.        ECHO PRESS ENTER KEY AFTER SELECTION IS MADE
  900.  
  901.                               Program name: 1.BAT
  902.  
  903.        ECHO OFF
  904.        REM Fourth line switches to a subdirectory containing your
  905.        REM word processor using the cd or change directory command
  906.        CD\WP
  907.        REM Seventh line starts your word processor, named word.exe
  908.        REM Substitute the startup command for your word processor
  909.        WORD
  910.        REM Eleventh line changes out of word processing subdirectory 
  911.        REM and goes back to the root directory when word processor
  912.        REM is finished
  913.        CD\
  914.        REM Final line restarts the menu program m.bat
  915.        M
  916.        
  917.                               Program name: 2.BAT
  918.  
  919.        ECHO OFF
  920.        REM Fourth line switches to a subdirectory containing your
  921.        REM spreadsheet using the cd or change directory command
  922.        CD\SPREAD
  923.        REM Seventh line starts your spreadsheet, named lotus.exe
  924.        REM Substitute the start command for your spreadsheet
  925.        LOTUS
  926.        REM Eleventh line changes out of spreadsheet subdirectory
  927.        REM and goes back to the root directory when spreadsheet
  928.        REM is finished
  929.        CD\
  930.        REM Final line restarts the menu program m.bat
  931.        M
  932.        
  933.                               Program name: 3.BAT
  934.        
  935.        ECHO OFF
  936.        REM Fourth line switches to a subdirectory containing your
  937.        REM database using the cd or change directory command
  938.        CD\DATABASE
  939.        REM Seventh line starts your database, named db.exe
  940.        REM Substitute the start command for your database
  941.        DB
  942.        REM Eleventh line changes out of database subdirectory
  943.        REM and goes back to the root directory when database
  944.        REM IS FINISHED
  945.        CD\
  946.        REM Final line restarts the menu program m.bat
  947.        M
  948.        
  949.                       ---- Printer Control Batch file ----
  950.        
  951.        Some of the best kept DOS secrets are fascinating. If you have a 
  952.        standard dot matrix printer connected to your computer, you can 
  953.        use simple batch files to change the typeface and other features 
  954.        your printer provides. The reference booklet which accompanied 
  955.        your printer discusses printer control codes. For example, on 
  956.        Epson compatible printers you will note in your printer book 
  957.        that "emphasized printing" has the DECIMAL control code 27 69. 
  958.        Let's write a batch file to take charge of our printer! 
  959.        
  960.                               Program name: E.BAT
  961.  
  962.        ECHO OFF
  963.        CLS
  964.        ECHO SETTING PRINTER TO EMPHASIZED MODE
  965.        ECHO (ALT 155)(ALT 69) >PRN
  966.        ECHO DONE
  967.  
  968.        The fourth line contains a secret trick. The code we need to 
  969.        send is 27 69 according to our printer book. When preparing this 
  970.        batch file with EDLIN, COPY CONsole or your word processor, you 
  971.        must send the printer control code 27 then 69 to the printer. 
  972.        
  973.        In line four after typing the word "ECHO" then a blank space, 
  974.        hold down the ALT key then press 155 on the FAR RIGHT NUMERIC 
  975.        KEYBOARD. When done entering the number, release the ALT key. 
  976.        On most computers, the cents symbol will appear which the 
  977.        printer will accept as the "escape code 27." DO NOT type the 
  978.        left and right parenthesis marks which appear on line four: ( ), 
  979.        they are only for clarity. 
  980.  
  981.        Next press ALT 69 which produces the E symbol. You could also 
  982.        just type capital E. Next type >PRN which sends this code 
  983.        to your printer. Note that there is NO blank space between (ALT 
  984.        155) and (ALT 69). 
  985.  
  986.        The code 155 is substituted for 27 but the 69 is unchanged. Why 
  987.        155 rather than 27 for the escape code? An explanation: Printer 
  988.        control codes begin with code 128, thus escape character 27 is 
  989.        generated by using 27+128 = 155. An necessary trick for this 
  990.        batch file. We could send SEVERAL codes by adding more lines to 
  991.        the batch file to set letter quality, pica font, line spacing 
  992.        and tabs, then type the letter to the printer (ECHO LETTER.TXT>PRN) 
  993.        and finally reset the printer. Your printer book discusses these 
  994.        decimal control codes. 
  995.  
  996.        If we wanted to send a formfeed to the printer (eject paper,) 
  997.        the printer reference book suggests control code 12, therefore 
  998.        12+128 = 140. So the fourth line in the batch file would read 
  999.        ECHO (ALT 140) >PRN. The bell sound (decimnal code 7) is 
  1000.        generated by 7+128 = 135. The batch file would change to read 
  1001.        ECHO (ALT 135) >PRN. 
  1002.        
  1003.        Some printer features are controlled by SINGLE control codes 
  1004.        while other features are controlled by MULTIPLE escape sequence 
  1005.        codes which always begin with 27 followed by additional numbers.
  1006.        Escape code 27 is always translated to ALT 155 when DOS and 
  1007.        batch files transmit the printer control information. Within 
  1008.        software programs the codes may use a different format: \027E
  1009.        for example.
  1010.  
  1011.        Many other printing features can be turned on using short batch 
  1012.        files. For example, double strike printing uses the decimal code 
  1013.        27 71. In the batch file you could use ECHO (ALT 155)(ALT 71) >PRN. 
  1014.        
  1015.        You can also turn on a COMBINATION of several features in one 
  1016.        longer batch file to control several features. Just add more 
  1017.        lines and codes to the batch file!
  1018.        
  1019.        To reset the printer when a printing job is finished: code 27 
  1020.        64. Search out these printer codes in your printer manual and 
  1021.        let a batch file do the hard work! 
  1022.  
  1023.        One of the best batch file tutorials to date was published in 
  1024.        two parts in the November and December 1991 editions of PC 
  1025.        Computing Magazine. Contact your library for back issues or 
  1026.        contact PC Computing at Back Issues Dept, PC Computing Magazine, 
  1027.        Ziff Davis, POB 53131, Boulder, CO 80322. Back issues currently 
  1028.        cost $6.00 each.
  1029.                          
  1030.        The bibliography/suggested reading list with PC-Learn provides 
  1031.        additional reading suggestions to advance your batch file 
  1032.        knowledge. 
  1033.  
  1034.        Tutorial finished. Have you registered PC-Learn to receive your
  1035.        bonus disks? Registration is encouraged. Shareware works on the
  1036.        honor system! Send $25 to Seattle Scientific Photography, 
  1037.        Department PCL5, PO Box 1506, Mercer Island, WA 98040. Latest 
  1038.        version of PC-Learn and two bonus disks shipped promptly!
  1039.        
  1040.        
  1041.